$(function () {

    //初始化编辑框
    initSNU({
        id:'affiche-create',
        type:1,
        toolsArr:null,
        height:300,
        gs:true,
        video:true
    });

    //初始化专业
    $.initSubject({
        element:'affiche-subject-box',//初始化专业select元素的ID
        context:'affiche-subject-context',//显示专业/科目名字内容ID
        initData:$('#subjectIds').val(),//初始化显示的专业数据，默认不设置
        removeCallback:function(parentId,subjectIds){//删除回调
            resetSubjectIds()
        },
        confirmCallback:function(parentId,subjectIds){//点击确认回调
            resetSubjectIds()
        }
    });
    /**
     * 重置专业ID串
     */
    function resetSubjectIds() {
        var _subjectIds = $.getAllSubjectIds("affiche-subject-context");
        $("input[name=subjectIds]").val(_subjectIds);
    }

    validateData();
    $("body").on('click','#selectCourse',function () {//选择课程
        selectCourse({
            selectType:"checkbox",
            type:1,
            callback:'selectCourseCallback'
        });
    });

    $('body').on('click', '.deleteCourse', function () {
        var dataId = $(this).parents('tr').data('id');
        var obj = $(this).parents('tr');
        deleteSelectCourse(dataId, obj)
    });

    //保存消息
    $('body').on('click', '#submitAffiche', function () {
        saveUpdateAffiche();
    });

    //取消操作
    $('body').on('click', '#onBack', function () {
        window.history.go(-1);
    });

    /**
     * 保存修改素材
     * @param status
     */
    function saveUpdateAffiche() {
        var afficheCreateFrom = $("#afficheCreateFrom").serializeJSON();
        afficheCreateFrom = JSON.stringify(afficheCreateFrom);
        $.ajax({
            url: baseloaction + "/admin/op/affiche/saveUpdateAffiche",
            data: afficheCreateFrom,
            dataType:"json",
            type: "post",
            contentType:'application/json;charset=utf-8',
            success: function (result) {
                if(result.success==true){
                    icModale(0,result.message);
                    window.location.href=baseloaction+"/admin/op/affiche/queryAfficheList";
                }else{
                    icModale(0, result.message);
                }
            }
        });
    }
});

function deleteSelectCourse(dataId, obj) {
    var courseIds = ',';
    var courses = $("#courseIds").val();
    var courseId = courses.split(',');
    for (var i = 0; i < courseId.length; i++) {
        if (courseId[i] != null && courseId[i] != '' && courseId[i] != dataId) {
            courseIds += courseId[i] + ",";
        }
    }
    if (courseIds.length > 1) {
        $("#courseIds").val(courseIds);
    } else {
        $("#courseIds").val("");
    }
    obj.remove();
}

/**
 * 选择课程回调
 * @param result 回调数据
 */
function selectCourseCallback(result) {
    if(result!=null && result.length>0){
        var courseIds = $('input[name="courseIds"]').val();
        if(courseIds==null||courseIds==''){
            courseIds =',';
        }
        $.each(result,function (index,course) {
            var courseName =course.courseName;
            var courseSubject=course.subject;
            var coursePrice =course.orPrice;
            if(courseIds.indexOf(","+course.id+",")==-1){
                $("#course-content-table-hd").removeClass("hide");
                courseIds +=course.id+',';
                $('#course-content').append(' <tr data-id="'+course.id+'">'+
                    '<td class="text-center">'+course.id+'</td>'+
                    '<td>'+courseName+'</td>'+
                    '<td>'+courseSubject+'</td>'+
                    '<td><span class="c-red">'+coursePrice+'</span></td>'+
                    '<td class="text-center"><a class="c-master deleteCourse"  href="javascript:void(0);">删除</a></td>'+
                    '</tr>');
            }
        });
        $('input[name="courseIds"]').val(courseIds);
    }
    parentId = 0;
}
